package com.etc.user.controller;

import com.etc.entity.OneLevel;
import com.etc.entity.ProductDisplay;
import com.etc.entity.TwoLevel;
import com.etc.service.OneLevelService;
import com.etc.service.ProductDisplayService;
import com.etc.service.TwoLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("user.action")
public class HomeController {
    @Autowired
    private OneLevelService oneLevelService;
    @Autowired
    private TwoLevelService twoLevelService;
    @Autowired
    private ProductDisplayService productDisplayService;

    /**
     *
     * @return 返回首页所需要的数据
     */
    @RequestMapping("home")
    public ModelAndView level(){
        System.out.println("有走到这里吗？");
        ModelAndView mv=new ModelAndView("user/home");
        List<OneLevel> listOne=oneLevelService.listOneLevel();
        List<TwoLevel> listTwo=twoLevelService.listTwoLevel();
        //得到面包下的商品
        List<ProductDisplay> listProduct2=productDisplayService.listProductDisplayById(2);
        if(listProduct2.size()>=4){
            listProduct2=listProduct2.subList(0,4);
        }
        //得到饼干下的商品
        List<ProductDisplay> listProduct5=productDisplayService.listProductDisplayById(5);
        if(listProduct5.size()>=4){
            listProduct5=listProduct5.subList(0,4);
        }
        //得到肉片下的商品
        List<ProductDisplay> listProduct8=productDisplayService.listProductDisplayById(8);
        if(listProduct8.size()>=4){
            listProduct8=listProduct8.subList(0,4);
        }
        mv.addObject("listOne",listOne);
        mv.addObject("listTwo",listTwo);
        mv.addObject("listProduct2",listProduct2);
        mv.addObject("listProduct5",listProduct5);
        mv.addObject("listProduct8",listProduct8);
        return mv;
    }

    /**
     *
     * @param twolevelid 二级分类的id
     * @return
     */
    @RequestMapping("productDisplay")
    public ModelAndView productDisplay(Integer twolevelid){
        ModelAndView mv=new ModelAndView("user/search");
        List<ProductDisplay> listProductDisplay=productDisplayService.listProductDisplayById(twolevelid);
        String twolevelname=twoLevelService.getTwoLevelById(twolevelid).getTwolevelname();
        Integer total=productDisplayService.getTotal(twolevelid);
        mv.addObject("listProductDisplay",listProductDisplay);
        mv.addObject("twolevelname",twolevelname);
        mv.addObject("total",total);
        return mv;
    }

    /**
     *
     * @param content 前台搜索输入的内容
     * @return 返回模糊查询的内容
     */
    @RequestMapping("toSearchByLike")
    public ModelAndView toSearchByLike(String content){
        ModelAndView mv=new ModelAndView("user/search");
        List<ProductDisplay> listProductDisplay=productDisplayService.listProductDisplayByLike(content);
        String twolevelname=content;
        Integer total=productDisplayService.getLikeTotal(content);
        mv.addObject("listProductDisplay",listProductDisplay);
        mv.addObject("twolevelname",twolevelname);
        mv.addObject("total",total);
        return mv;
    }
}
